﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Epitech.Krisk.Xna.UI.Game.InGame.Gameplay
{
    public class FightInfo : AGameObject
    {
        private Texture2D _soldier;
        private Rectangle _soldierrec;

        private Texture2D _tank;
        private Rectangle _tankrec;

        private Texture2D _warplane;
        private Rectangle _warplanerec;
        private Texture2D _won;
        private SpriteFont _font;
        private Vector2 _pos;
        private Vector2 _posText;
        private Army _atk;
        private Army _def;
        private String _text;

        public FightInfo(bool won, Army atk, Army def)
        {
            _text = won ? "Gagne !" : "Perdu !";
            _atk = atk;
            _def = def;
            ToDelete = false;
        }

        public bool ToDelete { get; set; }

        public override void LoadContent()
        {
            _soldier = Content.Load<Texture2D>("Game/Soldat_Sprite");
            _tank = Content.Load<Texture2D>("Game/Char_Sprite");
            _warplane = Content.Load<Texture2D>("Game/Avion_Sprite");
            _won = Content.Load<Texture2D>("Game/InGame/EndFight");
            _font = Content.Load<SpriteFont>("Font/defaultfont");

            _pos = new Vector2(ViewPort.Width / 2 - _won.Width / 2, ViewPort.Height / 2 - _won.Height / 2);
            _posText = new Vector2(ViewPort.Width / 2 - _font.MeasureString(_text).X / 2, _pos.Y + 25);
            _soldierrec = new Rectangle((int)(_pos.X + 56 - 8), (int)(_pos.Y + 280), 17, 45);
            _tankrec = new Rectangle((int)(_pos.X + 56 - 36), (int)(_pos.Y + 335), 72, 45);
            _warplanerec = new Rectangle((int)(_pos.X + 56 - 31), (int)(_pos.Y + 390), 63, 45);
        }

        private TimeSpan _begin = TimeSpan.Zero;

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            if (_begin == TimeSpan.Zero)
                _begin = gameTime.TotalGameTime;

            if (gameTime.TotalGameTime - _begin > TimeSpan.FromSeconds(3))
                ToDelete = true;
        }

        public override void Draw(GameTime gameTime)
        {
            Rectangle sold = new Rectangle(_soldierrec.X + 300, _soldierrec.Y, _soldierrec.Width, _soldierrec.Height);
            Rectangle tank = new Rectangle(_tankrec.X + 300, _tankrec.Y, _tankrec.Width, _tankrec.Height);
            Rectangle avi = new Rectangle(_warplanerec.X + 300, _warplanerec.Y, _warplanerec.Width, _warplanerec.Height);
            Vector2 pos;

            SpriteBatch.Begin();
            SpriteBatch.Draw(_won, _pos, Color.White);
            SpriteBatch.DrawString(_font, _text, _posText, new Color(208, 44, 6));
            SpriteBatch.Draw(_soldier, _soldierrec, Color.White);
            pos = new Vector2(_soldierrec.X + 150, _soldierrec.Y);
            SpriteBatch.DrawString(_font, _atk.Soldier.ToString(), pos, Color.Black);

            SpriteBatch.Draw(_tank, _tankrec, Color.White);
            pos = new Vector2(_soldierrec.X + 150, _tankrec.Y);
            SpriteBatch.DrawString(_font, _atk.Tank.ToString(), pos, Color.Black);

            SpriteBatch.Draw(_warplane, _warplanerec, Color.White);
            pos = new Vector2(_soldierrec.X + 150, _warplanerec.Y);
            SpriteBatch.DrawString(_font, _atk.WarPlane.ToString(), pos, Color.Black);

            SpriteBatch.Draw(_soldier, sold, Color.White);
            pos = new Vector2(sold.X + 150, sold.Y);
            SpriteBatch.DrawString(_font, _def.Soldier.ToString(), pos, Color.Black);

            SpriteBatch.Draw(_tank, tank, Color.White);
            pos = new Vector2(sold.X + 150, tank.Y);
            SpriteBatch.DrawString(_font, _atk.Tank.ToString(), pos, Color.Black);

            SpriteBatch.Draw(_warplane, avi, Color.White);
            pos = new Vector2(sold.X + 150, avi.Y);
            SpriteBatch.DrawString(_font, _atk.WarPlane.ToString(), pos, Color.Black);

            SpriteBatch.End();
        }
    }
}
